<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreUserRequest;
use App\Models\Labourer;
use App\Models\LabourerAttendance;
use App\Models\LabourerWage;
use App\Services\LabourerService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LabourerController extends Controller
{
    protected $LabourerService;
    public function __construct(LabourerService $LabourerService)
    {
        $this->LabourerService = $LabourerService;
    }

    public function index(Request $Request) {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;

        $sql = $this->LabourerService->get($search,$Request->project_id);
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function all(Request $Request) {
        $search = $Request->search;
        $sql = $this->LabourerService->get($search,$Request->project_id);
        $data = $sql->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function costs(Request $Request) {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;

        $sql = $this->LabourerService->get($search,$Request->project_id);
        $total = $sql->count();
        $data = $sql->with('wages')->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        foreach ($data as $k => $v) {
            $data[$k]->pay = 0;
            foreach ($v->wages as $key => $value) {
                $data[$k]->pay += $value->price;
            }
            $v->no_pay = $v->total_price - $v->pay;
        }
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Labourer::where('id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新用户。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreUserRequest $Request)
    {
        $POSTONLY = Labourer::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['password'] = $Request->password ?? '123456';
        $data = $this->LabourerService->insert($input,$Request->project_id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(StoreUserRequest $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = Labourer::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $input['password'] = $Request->password ?? '123456';
        $data = $this->LabourerService->update($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function attendance(Request $Request)
    {
        $month = date('m');
        $year = date('Y');
        $daysInMonth = date('t');
        $dates = array();

        for ($i = 1; $i <= $daysInMonth; $i++) {
            $date = $year . '-' . $month . '-' . str_pad($i, 2, '0', STR_PAD_LEFT);
            $dates[] = $date;
        }
        $count = count($dates);
        // $data = LabourerAttendance::select('id','labourer_id','attendance_date','in_time','out_time','status')->with('labourer')->whereDate('attendance_date','>=',$dates[0])->whereDate('attendance_date','<=',$dates[$count - 1])->where('project_id',$Request->project_id)->get();
        // $data = $data->groupBy('labourer_id');
        $data = Labourer::with('attendance')->where('project_id',$Request->project_id)->get();
        // foreach ($data as $key => $value) {
        //     $data[$key]->dates = $dates;
        // }
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function wage(Request $Request)
    {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;

        $sql = $this->LabourerService->wage($search,$Request->project_id);
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function wageShow(Request $Request)
    {
        $data = LabourerWage::with('labourer')->where('id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function storeWage(Request $Request)
    {
        $POSTONLY = LabourerWage::POSTONLY;
        $input = $Request->only($POSTONLY);
        $data = $this->LabourerService->insertWage($input,$Request->project_id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function updateWage(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = LabourerWage::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $data = $this->LabourerService->updateWage($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    // $month = date('m');
    //     $year = date('Y');
    //     $daysInMonth = date('t');
    //     $dates = array();
    //     $arr_ = [];
    //     for ($i = 1; $i <= $daysInMonth; $i++) {
    //         $date = $year . '-' . $month . '-' . str_pad($i, 2, '0', STR_PAD_LEFT);
    //         $dates[] = $date;
    //         $arr = [];
    //         $arr['labourer_id'] = 3;
    //         $arr['project_id'] = 1;
    //         $arr['attendance_date'] = $date;
    //         $arr['in_time'] = '08:00:00';
    //         $arr['out_time'] = '18:00:00';
    //         $arr['status'] = 1;
    //         $arr_[] = $arr;
    //     }

    //     DB::table('labourer_attendances')->insert($arr_);



}
